/*
 *  CommentPanel.java
 *
 *  $Id: CommentPanel.java 128 2011-02-15 12:45:43Z johann.petrak $
 *
 *  Copyright Austrian Research Institute for Artificial Intelligence (OFAI)
 *  http://www.ofai.at
 *
 *  Licensed under the GNU General Public License Version 2
 *
 */
package at.ofai.gate.appdoc;

import gate.Executable;
import gate.FeatureMap;
import gate.event.FeatureMapListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JPanel;

/**
 * The jpanel object that does the actual work for editing the @comment,
 * @author, and @version fields and storing their values in the Executable's
 * feature map for later use in the generated documentation.
 *
 * @author Johann Petrak
 */
public class CommentPanel
  extends JPanel
  implements FocusListener, FeatureMapListener {

    AppDoc owner;
    Executable target;
    FeatureMap featureMap;
    /** Creates new form CommentPanel
     * @param theOwner 
     */
    public CommentPanel(AppDoc theOwner) {
        owner = theOwner;
        initComponents();
        initData();
    }

    private void initData() {
        target = (Executable) owner.theTarget;
        featureMap = owner.theTarget.getFeatures();
        featureMap.addFeatureMapListener(this);
        jTextFieldAuthor.setName("author");
        jTextFieldAuthor.addFocusListener(this);
        jTextFieldVersion.setName("version");
        jTextFieldVersion.addFocusListener(this);
        jTextAreaComment.setName("comment");
        jTextAreaComment.addFocusListener(this);
        FeatureMap fm = owner.theTarget.getFeatures();
        //System.out.println("Feature map is "+fm);
        String comment = getFMValue("@comment");
        if(comment == null) {
          comment = "";
        }
        jTextAreaComment.setText(comment);
        String author = getFMValue("@author");
        if(author == null) {
          author = "";
        }
        jTextFieldAuthor.setText(author);
        String version = getFMValue("@version");
        if(version == null) {
          version = "";
        }
        jTextFieldVersion.setText(version);

    }

    private String getFMValue(String key) {
      String tmp = (String)featureMap.get(key);
      if(tmp == null) {
        return "";
      } else {
        return tmp;
      }
    }

    public void featureMapUpdated() {
      if(!getFMValue("@comment").equals(jTextAreaComment.getText())) {
        jTextAreaComment.setText(getFMValue("@comment"));
      } else if(!getFMValue("@author").equals(jTextFieldAuthor.getText())) {
        jTextFieldAuthor.setText(getFMValue("@author"));
      } else if(!getFMValue("@version").equals(jTextFieldVersion.getText())) {
        jTextFieldVersion.setText(getFMValue("@version"));
      }
    }
    
    public void focusGained(FocusEvent e) {}
    public void focusLost(FocusEvent e) {
      //System.out.println("Focus lost for "+e.getComponent().getName());
      String field = e.getComponent().getName();
      if(field.equals("comment")) {
        owner.theTarget.getFeatures().put("@comment", jTextAreaComment.getText());
      } else if(field.equals("author")) {
        owner.theTarget.getFeatures().put("@author", jTextFieldAuthor.getText());
      } else if(field.equals("version")) {
        owner.theTarget.getFeatures().put("@version", jTextFieldVersion.getText());
      }
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTextAreaComment = new javax.swing.JTextArea();
    jTextFieldAuthor = new javax.swing.JTextField();
    jTextFieldVersion = new javax.swing.JTextField();

    jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Comment"));

    jTextAreaComment.setColumns(20);
    jTextAreaComment.setRows(5);
    jTextAreaComment.setBorder(null);
    jScrollPane1.setViewportView(jTextAreaComment);

    jTextFieldAuthor.setBorder(javax.swing.BorderFactory.createTitledBorder("Author"));

    jTextFieldVersion.setBorder(javax.swing.BorderFactory.createTitledBorder("Version"));
    jTextFieldVersion.setMinimumSize(new java.awt.Dimension(40, 60));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
            .addComponent(jTextFieldAuthor, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jTextFieldVersion, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jTextFieldAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jTextFieldVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea jTextAreaComment;
  private javax.swing.JTextField jTextFieldAuthor;
  private javax.swing.JTextField jTextFieldVersion;
  // End of variables declaration//GEN-END:variables

}
